package tp06.example01;

import com.kaifa.org.tp06.example01.bean.Student;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.Reader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class StudentLogin {
    private SqlSessionFactory sqlSessionFactory;
    private SqlSession sqlSession;

//    初始化，在执行每一个测试方法之前，都会先执行初始化init()方法
    @Before
    public void init() throws IOException {
        String resource = "mybatis-config.xml";
        Reader reader = Resources.getResourceAsReader(resource);
        sqlSessionFactory =new SqlSessionFactoryBuilder().build(reader);
        sqlSession = sqlSessionFactory.openSession();
    }
    @Test
    public void findStuTest(){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("stuClass","开发1班");
        List<Student> list=sqlSession.selectList("findStu",map);
        for (Student s:list) {
            System.out.println(s);
        }
    }
    @Test
    public void deleteStuTest(){
        int i=sqlSession.insert("deleteStu",3);
        if(i>0){
            System.out.println("删除成功");
        }else{
            System.out.println("删除失败");
        }
    }
    @Test
    public void addStuTest(){
        Student student = new Student();
        student.setStuClass("开发2班");
        student.setStuName("赵六");
        student.setStuUserName("zl");
        student.setStuUserPwd("111111");
        int i=sqlSession.insert("addStudent",student);
        if(i>0){
            System.out.println("添加成功");
        }else{
            System.out.println("添加失败");
        }

    }
    @Test
    public void updateStuTest(){
        Student student = new Student();
        student.setStuClass("开发2班");
        student.setStuName("王五");
        student.setStuUserName("zl");
        student.setStuUserPwd("111111");
        student.setId(2);

        int i = sqlSession.update("updateStu",student);
        if(i>0){
            System.out.println("修改成功");
        }else{
            System.out.println("修改失败");
        }

    }
//    在执行完测试方法之后，都会执行一次destroy()方法
    @After
    public void destroy(){
//        提交事务
        sqlSession.commit();
//        关闭事务
        sqlSession.close();
    }



    @Test
    public void login() throws IOException {
        String resource = "mybatis-config.xml";
        Reader reader = Resources.getResourceAsReader(resource);
        SqlSessionFactory sqlSessionFactory =new SqlSessionFactoryBuilder().build(reader);
        SqlSession session = sqlSessionFactory.openSession();
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("stuUserName","zs");
        map.put("pwd","123456");
        Student student = session.selectOne("findByNameAndPwd",map);
        session.close();
    }





}
